<?php
/**
 * +----------------------------------------------------------------------
 * | 权限管理控制器
 * +----------------------------------------------------------------------
 *                      .::::.
 *                    .::::::::.            | AUTHOR: siyu
 *                    :::::::::::           | EMAIL: 407593529@qq.com
 *                 ..:::::::::::'           | QQ: 407593529
 *             '::::::::::::'               | WECHAT: zhaoyingjie4125
 *                .::::::::::               | DATETIME: 2019/03/27
 *           '::::::::::::::..
 *                ..::::::::::::.
 *              ``::::::::::::::::
 *               ::::``:::::::::'        .:::.
 *              ::::'   ':::::'       .::::::::.
 *            .::::'      ::::     .:::::::'::::.
 *           .:::'       :::::  .:::::::::' ':::::.
 *          .::'        :::::.:::::::::'      ':::::.
 *         .::'         ::::::::::::::'         ``::::.
 *     ...:::           ::::::::::::'              ``::.
 *   ```` ':.          ':::::::::'                  ::::..
 *                      '.:::::'                    ':'````..
 * +----------------------------------------------------------------------
 */
namespace app\admin\controller;
use app\admin\model\AuthGroup;
use app\admin\model\AuthRule;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;

class Role extends Base
{
    public function index(){

        if(Request::isAjax())
        {
            $roleModel = new AuthGroup();
            $data = input('param.');
            return $roleModel->tableData($data);
        }
        return View::fetch('index');

    }
    /**
     * 添加
     * @return array|mixed
     */
    public function add()
    {
        $result = [
            'status' => false,
            'msg' => '失败',
            'data' => ''
        ];
        $roleModel = new AuthGroup();
        if(Request::isPost())
        {
            if(!input('?param.title'))
            {
                return error_code(11070);
            }
            $data['title'] = input('param.title');
            $roleModel ->save($data);
            $result['status'] = true;
            $result['msg'] = '添加成功';
            return $result;
        }
        $result['status'] = true;
        $result['msg'] = '成功';
        $result['data'] = View::fetch('add');
        return $result;
    }

    /**
     * 角色权限修改
     */
    public function edit(){



        //halt($tree_data);
        $view = [
            'id' =>Request::param('id'),
        ];

        View::assign($view);
//        return View::fetch();

        $return['status'] = true;
        $return['msg'] = '成功';
        $return['data'] = View::fetch();
        return $return;


    }

    public function getTree(){
        $role_auth_list = Db::name('auth_group')
            ->where('id', Request::param('id'))
            ->value('rules');
        $role_auth_list=explode(',', $role_auth_list);
        foreach ($role_auth_list as $k=>$v){
            $role_auth_list[$k]=intval($v);
        }
        $auth_rule=new AuthRule();
        $auth_list=$auth_rule->field('id,pid,title')->select();
        $tree_data = [

            'list' => $auth_list->toArray(),
            'checkedId' =>$role_auth_list,

        ];
        $tree_data=json_encode($tree_data,JSON_UNESCAPED_UNICODE);
        return $tree_data;
    }

    /**
     * @return array|mixed
     * 保存节点
     */
    public function savePerm()
    {
        $post = input('param.');

        //halt($post);
        if(!isset($post['id']))
        {
            return error_code(10000);
        }
        if(!isset($post['data']))
        {
            return error_code(10000);
        }
        //保存角色信息
        $roleModel = new AuthGroup();
        $roleInfo = $roleModel->where(['id'=>$post['id']])->find();
        if(!$roleInfo)
        {
            return error_code(11071);
        }
        $authGroupModel = new AuthGroup();

        //halt($post['id']);
        $post['data']=implode(',',$post['data']);
        $authGroupModel->savePerm($post['id'],$post['data']);
        return [
            'status' => true,
            'data' => '',
            'msg' => '设置成功'
        ];
    }

    public function del(){
        $id = Request::param('id');
        if ($id) {
            AuthGroup::destroy($id);
            return [
                'status' => true,
                'data' => '',
                'msg' => '删除成功'
            ];
        }
    }




}
